-- StartAction
-- create by weism
-- 开始动作

StartAction = class("StartAction", function()
    return ActionBase.create();
end);
function StartAction.create(source, target, skillId)
    local self = StartAction.new();
    self.source = source;
    self.target = target;
    self.skillId = skillId;
    return self;
end

-- 构造函数
function StartAction:ctor()
end

-- 开始播放
function StartAction:play()
    local delay = 0;
    if self.source == ME.user then
        -- 玩家攻击怪物
        delay = self:heroStartAction();
    elseif self.source.type == OBJECT_TYPE_MONSTER and
           self.target.type == OBJECT_TYPE_USER then
        -- 怪物攻击玩家
        self:monsterStartAction();

        -- 怪物攻击玩家，后续操作不需要等待
        delay = 0;
    elseif self.source.type == OBJECT_TYPE_BINE then
        -- 魔藤攻击怪物
        delay = self:bineStartAction();
    end

    return delay;
end

-- 玩家攻击怪物
function StartAction:heroStartAction()
--    do
--        -- 攻击暂时光效不要了
--
--        -- 播放英雄攻击动作
--        UIDungeonMgr:getCurLevel():switchHeroAction(HERO_ACTION_ATTACK);
--
--        -- TODO 暂时播放1秒，需要改成读取配置
--        return 0;
--    end

    -- 播放英雄攻击动作
    local actionType = HERO_ACTION_ATTACK;
    if self.skillId ~= 0 then
        if not SkillM.needTarget(self.skillId) then
            -- 播放对自身施法攻击动作
            actionType = HERO_ACTION_CAST_ME;
        else
            -- 播放对他人施法动作
            actionType = HERO_ACTION_CAST_OTHER;
        end
    end

    if not UIDungeonMgr:getCurLevel() then
        return;
    end

    UIDungeonMgr:getCurLevel():switchHeroAction(actionType);

    if self.skillId == PHYSIC_ATTACK then
        return;
    end

    -- 光效播放父节点
    local effectParent = UIDungeonMgr:getCurLevel():getEffectParent();

    -- 起手光效
    local prepareEffectId = SkillM.query(self.skillId, "prepare_effect");

    -- 起始位置
    local fromPos = UIDungeonMgr:getCurLevel():getHeroPos();

    -- 先播放起手光效
    if type(prepareEffectId) == "number" then
        playEffect(effectParent, prepareEffectId, fromPos.x, fromPos.y);
    end

    -- 飞行光效
    local bulletEffectId = SkillM.query(self.skillId, "bullet_effect");

    local duration = 0;
    if type(bulletEffectId) == "number" then

        -- 目标位置
        local toPos = UIDungeonMgr:getCurLevel():getMonsterPos(self.target:getPos());

        -- 飞行时间
        duration = cc.pGetDistance(fromPos, toPos) / 1800;

        -- 计算水平射线需要旋转的角度
        -- 由于setRotation是顺时针旋转的，因此需要取反
        local angle = -calcAngle(fromPos, toPos);

        local node = cc.Node:create();

        effectParent:addChild(node);
        local _, effect = playEffect(node, bulletEffectId, 0, 0);
        node:setPosition(fromPos);
        node:setRotation(angle+90);

        local function stopAction()
            effectParent:removeChild(node);
        end

        local callfunc = cc.CallFunc:create(stopAction)

        local moveTo = cc.MoveTo:create(duration, toPos);
        node:runAction(cc.Sequence:create( moveTo, callfunc));
    end

    -- 场景光效(全屏)
    local sceneEffectId = SkillM.query(self.skillId, "scene_effect");
    local frameSize = AlignM.frameSize;
    if type(sceneEffectId) == "number" then
        -- 有全屏的光效， 延迟按这个来
        duration, _ = playEffect(effectParent, sceneEffectId, 320, 480);
    end

    local dbase = SkillM.query(self.skillId, "dbase");

    -- 单技能等待时间（如死亡波纹）
    if dbase and dbase.wait_time and dbase.wait_time > 0 then
        duration = dbase.wait_time;
    end

    -- 是否配置了锁屏时间
    if dbase ~= nil and dbase.lock_screen_time ~= nil and dbase.lock_screen_time > 0 then
        wait(dbase.lock_screen_time);
    end

    return duration;
end

-- 怪物攻击玩家
function StartAction:monsterStartAction()
--    do
--        -- 攻击暂时光效不要了
--        return 0;
--    end

    local fromPos = UIDungeonMgr:getCurLevel():getMonsterPos(self.source:getOwner());
    local toPos = UIDungeonMgr:getCurLevel():getHeroPos();

    local duration = cc.pGetDistance(fromPos, toPos) / 2000;

    local effect = ccui.ImageView:create();
    effect:loadTexture("images/ui/level/item/bullet.png");
    effect:setPosition(fromPos);
    local moveTo = cc.MoveTo:create(duration, toPos);
    local rotate = cc.RotateBy:create(duration, 360);

    local function stopAction()
        UIDungeonMgr:getCurLevel():getEffectParent():removeChild(effect);
    end

    local callfunc = cc.CallFunc:create(stopAction)

    UIDungeonMgr:getCurLevel():getEffectParent():addChild(effect);
    effect:runAction(cc.Sequence:create(cc.Spawn:create(moveTo, rotate), callfunc));

    -- 场景光效(全屏)
    local sceneEffectId = SkillM.query(self.skillId, "scene_effect");

    if type(sceneEffectId) == "number" then
        -- 有全屏的光效， 延迟按这个来
        local effectParent = UIDungeonMgr:getCurLevel():getEffectParent();
        playEffect(effectParent, sceneEffectId, 320, 480);
    end

    return duration;
end

-- 魔藤攻击怪物
function StartAction:bineStartAction()
    -- TODO
end

return StartAction;